const app = getApp();
 
/*
小程序利用canvas实现一键保存图片功能 */
Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    pir:"18888.00"
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.bgimg();
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },
 
  /**
   * 生命周期函数--监听页面显示 
   */
  onShow: function () {
    
  },
 
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },
 
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },
 
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },
 
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },
 
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  canvasImgFun:function(){ 
    
  },
  bgimg:function(){
    var that=this;
    const ctx=wx.createCanvasContext('myCanvas',this);
    let width=280;
    let height=600;
    let bgPicturePath = "http://47.106.193.134:8082/image/change.jpg";
    ctx.drawImage(bgPicturePath,0,0,width,height);
    let money="../../../images/money.png";
    const str=that.data.pir;
    ctx.fillStyle = '#151515';
    ctx.font="normal bold 20px Arial,sans-serif";
    ctx.fillText(str, (width-ctx.measureText(str).width)*0.5+6, 175);
    ctx.drawImage(money,(width-ctx.measureText(str).width)*0.5-10,160);
    
    ctx.draw();
  },
  fontext:function(){
  
  },
  savecanvas(){
    let _this=this
    wx.canvasToTempFilePath({
      x: 0,
      y: 0,
      width: 280,
      height: 600,
      canvasId: 'myCanvas',
      success:function(res) {
        console.log(res,'ssss')
        let img = res.tempFilePath
        wx.saveImageToPhotosAlbum({
          filePath: img,
          success(json) {
            wx.showToast({
              title:'成功保存',
              icon: 'none',
              duration: 2000
            });
            _this.cancelcanvas()
            console.log(json, 'kk')
          },
          fail() {
            wx.showToast({
              title: '保存失败',
              icon: 'none',
              duration: 2000
            });
            _this.cancelcanvas()
          }
        })
        console.log(res.tempFilePath)
      }
    })
  },
  inputpir:function(e){
    var that=this;
    let a=e.detail.value;
    let b=parseFloat(a);
    let c=b.toFixed(2);
    let str=c;
    that.setData({
      pir:str
    })
    that.bgimg();
  }
})
